package controller.menubar.commands;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JOptionPane;

import model.GlobalParams;

import view.MainFrame;

public class EditFile implements ActionListener{
	
	public EditFile(MainFrame frame) {		
		this.frame = frame;		
	}
	
	
	private MainFrame frame;

	@Override
	public void actionPerformed(ActionEvent arg0) {
		File selected = frame.getMainPanel().getPrimary().getDiskContent().getSelectedFile();
		
		//WatchService 
		
		if (Desktop.isDesktopSupported()) {		
			try {
				Desktop.getDesktop().edit(selected);
			} catch (IOException e) {			
				
				int option = JOptionPane.showConfirmDialog(frame, GlobalParams.getInstance().stringCollection.getOther_file_edit_error(), GlobalParams.getInstance().stringCollection.getOther_operation_manager(), JOptionPane.YES_NO_OPTION);
                if(option != JOptionPane.YES_OPTION) 
                	return;
                else  {
                	
                	try {  
                        Runtime.getRuntime().exec("notepad.exe " + selected.getAbsolutePath());  
                        }  
                      catch(Exception e1) {  
                        e.printStackTrace();          
                        }  
                	
                }
                	return;				
				
			}
		}
		
	}
	
}
